package com.jcl.recruit.timer.crawler.service.impl;

import com.jcl.recruit.resume.model.ResumeOrig;
import com.jcl.recruit.searchresume.model.SearchResume;
import com.jcl.recruit.searchresume.model.SearchResumeEdu;
import com.jcl.recruit.searchresume.model.SearchResumeProject;
import com.jcl.recruit.searchresume.model.SearchResumeWork;
import com.jcl.recruit.timer.crawler.bean.JxBean;
import com.jcl.recruit.timer.crawler.bean.JxResumeBean;
import com.jcl.recruit.timer.crawler.service.CrawlerChannelResumeJx;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName crawlerChannelResumeTaskService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/30 15:57
 * @Version 1.0
 **/
public class BossCrawlerResumeTask implements CrawlerChannelResumeJx {

    @Override
    public JxBean crawlerResume(List<ResumeOrig> resumeOrigList) throws Exception{

        JxBean jxBean = new JxBean();
        List<JxResumeBean> jxResumeBeans = new ArrayList<>();
        jxBean.setJxResumeBeans(jxResumeBeans);

        for(ResumeOrig entity: resumeOrigList) {

            jxBean.setExternalId(entity.getId());
            JxResumeBean jxResumeBean = new JxResumeBean();
            jxResumeBeans.add(jxResumeBean);

            SearchResume searchResume = new SearchResume();
            Document doc = Jsoup.parse(entity.getContent());
            Elements lables = doc.select("span.label-text");
            Elements citys = doc.select("p.gray");
            searchResume.setCurrentCity(citys.toString().substring(citys.toString().indexOf("</em>") + 5, citys.toString().indexOf("<em", citys.toString().indexOf("<em") + 1)));
            searchResume.setCurrentAddress(searchResume.getCurrentCity());
            searchResume.setNativePlace(searchResume.getCurrentAddress());
            // 如果第一个字符是年，则没有年龄。
            if (lables.get(0).text().contains("年")) {
                lables.add(0, null);
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                Integer birthYear = Integer.valueOf(sdf.format(new Date())) - Integer.valueOf(lables.get(0).text().substring(0, lables.get(0).text().length() - 1));
                searchResume.setBirthday(sdf.parse(birthYear.toString(), new ParsePosition(0)));
            }
            searchResume.setWorkyear(Integer.valueOf(lables.get(1).text().split("\\D+")[0]));
            searchResume.setEduLastEducation(lables.get(2).text());
            searchResume.setArrivalInfo(lables.get(3).text());
            // 期望薪资有可能是面议
            if (lables.get(6).text().contains("k")) {
                String salaryStart = lables.get(6).text().substring(0, lables.get(6).text().indexOf("k"));
                // 截止薪资可能是多少薪资以上
                if (lables.get(6).text().contains("-")) {
                    String salaryEnd = lables.get(6).text().substring(lables.get(6).text().indexOf("-") + 1, lables.get(6).text().lastIndexOf("k"));
                    searchResume.setDesireSalaryEnd(Integer.valueOf(salaryEnd) * 1000);
                }
                searchResume.setDesireSalaryStart(Integer.valueOf(salaryStart) * 1000);
            }
            searchResume.setSourceSub("zhipin");
            searchResume.setCreatetime(new Date());
            searchResume.setUpdateDate(searchResume.getCreatetime());
            searchResume.setSource(0);
            searchResume.setStatus(0);
            Elements text = doc.select("div.text");
            searchResume.setEvaluate(text.first().text());
            searchResume.setKeyword(entity.getKeyword());

            jxResumeBean.setSearchResume(searchResume);

            Elements historys = doc.select("div.resume-item");
            for (Element p : historys) {
                if (p.select("h3.title").text().equals("工作经历")) {
                    List<SearchResumeWork> resumeWorks = new ArrayList<>();
                    for (int i = 0; i < p.select("h4.name").size(); i++) {
                        if (p.select("h4.name").eq(i) != null) {
                            SearchResumeWork searchResumeWork = new SearchResumeWork();
                            if (StringUtils.isBlank(p.select("div.history-item").select("span.period").eq(i).text())) {
                                Integer n = 0;
                                for (int j = i; j >= 0; j--) {
                                    if (StringUtils.isNotBlank(p.select("div.history-item").select("span.period").eq(j).text())) {
                                        n = j;
                                    }
                                }
                                searchResumeWork.setStartDate(p.select("div.history-item").select("span.period").eq(n).text().substring(0, p.select("div.history-item").select("span.period").eq(n).text().indexOf("-")));
                                searchResumeWork.setEndDate(p.select("div.history-item").select("span.period").eq(n).text().substring(p.select("div.history-item").select("span.period").eq(n).text().indexOf("-")) + 1);
                            } else {
                                searchResumeWork.setStartDate(p.select("div.history-item").select("span.period").eq(i).text().substring(0, p.select("div.history-item").select("span.period").eq(i).text().indexOf("-")));
                                searchResumeWork.setEndDate(p.select("div.history-item").select("span.period").eq(i).text().substring(p.select("div.history-item").select("span.period").eq(i).text().indexOf("-")) + 1);
                            }
                            searchResumeWork.setDuty(p.select("div.history-item").select("div.text").eq(i).text());
                            String Object = p.select("div.history-item").select("h4.name").eq(i).toString();
                            // 如果公司名字相同这边会有问题
                            if (StringUtils.isBlank(Object)) {
                                Object = p.select("div.history-item").select("h4.name").eq(0).toString();
                            }
                            searchResumeWork.setCompany(Object.substring(Object.indexOf("class='name'>") + 18, Object.indexOf("<em class")));
                            searchResumeWork.setPosition(Object.substring(Object.indexOf("</em>") + 5, Object.indexOf("</h4")));
                            resumeWorks.add(searchResumeWork);
                        }
                    }
                    jxResumeBean.setResumeWorks(resumeWorks);
                }
                if (p.select("h3.title").text().equals("项目经验")) {
                    List<SearchResumeProject> projects = new ArrayList<>();
                    for (int i = 0; i < p.select("h4.name").size(); i++) {
                        if (p.select("h4.name").eq(i) != null) {
                            SearchResumeProject searchResumeProject = new SearchResumeProject();
                            // 这边jsoup选择器会自动去重。所以如果遇到为空的情况则取上一个
                            if (StringUtils.isBlank(p.select("div.history-item").select("span.period").eq(i).text())) {
                                Integer m = 0;
                                for (int j = i; j >= 0; j--) {
                                    if (StringUtils.isNotBlank(p.select("div.history-item").select("span.period").eq(j).text())) {
                                        m = j;
                                    }
                                }
                                searchResumeProject.setStartDate(p.select("div.history-item").select("span.period").eq(m).text().substring(0, p.select("div.history-item").select("span.period").eq(m).text().indexOf("-")));
                                searchResumeProject.setEndDate(p.select("div.history-item").select("span.period").eq(m).text().substring(p.select("div.history-item").select("span.period").eq(m).text().indexOf("-")) + 1);
                            } else {
                                searchResumeProject.setStartDate(p.select("div.history-item").select("span.period").eq(i).text().substring(0, p.select("div.history-item").select("span.period").eq(i).text().indexOf("-")));
                                searchResumeProject.setEndDate(p.select("div.history-item").select("span.period").eq(i).text().substring(p.select("div.history-item").select("span.period").eq(i).text().indexOf("-")) + 1);
                            }
                            String Object = p.select("div.history-item").select("h4.name").eq(i).toString();
                            if (StringUtils.isBlank(Object)) {
                                Object = p.select("div.history-item").select("h4.name").eq(0).toString();
                            }
                            searchResumeProject.setProjectname(Object.substring(Object.indexOf("class='name'>") + 18, Object.indexOf("<em class")));
                            searchResumeProject.setDuty(Object.substring(Object.indexOf("</em>") + 5, Object.indexOf("</h4")));
                            searchResumeProject.setDescription(p.select("div.history-item").select("div.text").eq(i).text());
                            projects.add(searchResumeProject);
                        }
                    }

                    jxResumeBean.setResumeProjects(projects);
                }
                if (p.select("h3.title").text().equals("教育经历")) {
                    List<SearchResumeEdu> edus = new ArrayList<>();
                    for (int i = 0; i < p.select("h4.name").size(); i++) {
                        if (p.select("h4.name").eq(i) != null) {
                            SearchResumeEdu searchResumeEdu = new SearchResumeEdu();
                            if (StringUtils.isBlank(p.select("div.history-item").select("span.period").eq(i).text())) {
                                Integer o = 0;
                                for (int j = i; j >= 0; j--) {
                                    if (StringUtils.isNotBlank(p.select("div.history-item").select("span.period").eq(j).text())) {
                                        o = j;
                                    }
                                }
                                searchResumeEdu.setStartDate(p.select("div.history-item").select("span.period").eq(o).text().substring(0, p.select("div.history-item").select("span.period").eq(o).text().indexOf("-")));
                                searchResumeEdu.setEndDate(p.select("div.history-item").select("span.period").eq(o).text().substring(p.select("div.history-item").select("span.period").eq(o).text().indexOf("-")) + 1);
                            } else {
                                searchResumeEdu.setStartDate(p.select("div.history-item").select("span.period").eq(i).text().substring(0, p.select("div.history-item").select("span.period").eq(i).text().indexOf("-")));
                                searchResumeEdu.setEndDate(p.select("div.history-item").select("span.period").eq(i).text().substring(p.select("div.history-item").select("span.period").eq(i).text().indexOf("-")) + 1);
                            }
                            Elements Object = p.select("div.history-item").select("h4.name").eq(i);
                            if (StringUtils.isBlank(Object.toString())) {
                                Object = p.select("div.history-item").select("h4.name").eq(0);
                            }
                            searchResumeEdu.setSchool(Object.select("b").text());
                            // 大学以下文化没有专业
                            if (Object.toString().indexOf("em>") - Object.toString().lastIndexOf("<em") < 0) {
                                searchResumeEdu.setMajor(Object.toString().substring(Object.toString().indexOf("em>") + 3, Object.toString().lastIndexOf("<em")));
                            }
                            searchResumeEdu.setDegree(Object.toString().substring(Object.toString().lastIndexOf("em>") + 3, Object.toString().lastIndexOf("</h4")));
                            edus.add(searchResumeEdu);
                        }
                    }

                    jxResumeBean.setSearchResumeEdus(edus);
                }
            }
        }
        return jxBean;
    }


}
